package com.buddies.lottery.ui.lottery.bet.detail;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.buddies.lottery.App;
import com.buddies.lottery.R;
import com.buddies.lottery.base.BaseActivity;
import com.buddies.lottery.data.bean.LotteryBetRecord;
import com.buddies.lottery.utils.DateUtils;
import com.buddies.lottery.utils.LotteryUtils;
import com.buddies.lottery.widget.DotView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.r0adkll.slidr.Slidr;

public class LotteryBetDetailActivity extends BaseActivity<LotteryBetDetailContract.Presenter> implements LotteryBetDetailContract.View {

    protected LotteryBetRecord mLotteryBetRecord;

    protected SwipeRefreshLayout mSwipeRefreshLayout;

    protected TextView tvMoney;

    private SimpleDraweeView image;
    protected TextView tvCategory;
    protected TextView tvPeriod;

    protected DotView mWinNumView;
    protected DotView mWinNumAnnotationView;
    protected TextView tvDuration;

    protected TextView tvContent;
    protected TextView tvTime;

    protected TextView tvStatus;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Slidr.attach(this, App.getSlidrConfig());
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_lottery_bet_detail;
    }

    @Override
    protected void initView() {
        setUp();

        mSwipeRefreshLayout = findViewById(R.id.refresher);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                forceRefresh();
            }
        });

        tvMoney = findViewById(R.id.tv_money);

        image = findViewById(R.id.image);
        tvCategory = findViewById(R.id.tv_category);
        tvPeriod = findViewById(R.id.tv_period);

        mWinNumView = findViewById(R.id.tv_win_num);
        mWinNumAnnotationView = findViewById(R.id.tv_win_num_annotation);
        tvDuration = findViewById(R.id.tv_duration);

        tvContent = findViewById(R.id.tv_content);
        tvTime = findViewById(R.id.tv_time);

        tvStatus = findViewById(R.id.tv_status);
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        if (intent != null) {
            mLotteryBetRecord = (LotteryBetRecord) intent.getSerializableExtra("bet");
        }

        if (mLotteryBetRecord == null || mLotteryBetRecord.id == 0) {
            finish();
            return;
        }

        setTitle("投注详情 " + mLotteryBetRecord.id);

        mPresenter = new LotteryBetDetailPresenter(this);

        onLotteryBetDetailLoaded(mLotteryBetRecord);

        forceRefresh();
    }

    private void forceRefresh() {
        if (mSwipeRefreshLayout != null) {
            mSwipeRefreshLayout.post(new Runnable() {
                @Override
                public void run() {
                    mSwipeRefreshLayout.setRefreshing(true);
                    mPresenter.loadLotteryBetDetail(mLotteryBetRecord.id);
                }
            });
        }
    }

    @Override
    public void onLotteryBetDetailLoaded(LotteryBetRecord betRecord) {
        mSwipeRefreshLayout.setRefreshing(false);
        if (betRecord != null && betRecord.id != 0) {

            tvMoney.setText(betRecord.getWinMoney());

            image.setImageURI(betRecord.lottery.pic_url);
            tvCategory.setText(betRecord.lottery.name);
            tvPeriod.setText(LotteryUtils.formatLotteryPeriod(betRecord.period));

            if (betRecord.lottery.next != null && !TextUtils.isEmpty(betRecord.lottery.next.win_num)) {
                mWinNumView.setDots(betRecord.lottery.id, betRecord.lottery.next.getWinNum());
                mWinNumAnnotationView.setDots(betRecord.lottery.next.getWinNumAnnotation());
                tvDuration.setVisibility(View.GONE);
                mWinNumView.setVisibility(View.VISIBLE);
                mWinNumAnnotationView.setVisibility(View.VISIBLE);
            } else {
                tvDuration.setVisibility(View.VISIBLE);
                mWinNumView.setVisibility(View.GONE);
                mWinNumAnnotationView.setVisibility(View.GONE);
            }

            tvContent.setText(String.format("%s @%sx%s", betRecord.option.name, betRecord.option.odds, betRecord.money));
            tvTime.setText(DateUtils.format(betRecord.create_at * 1000));

            tvStatus.setText(betRecord.getStatus());
        }
    }

    @Override
    public void onError(Throwable e) {
        super.onError(e);
        mSwipeRefreshLayout.setRefreshing(false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public static void launch(Context context, LotteryBetRecord betRecord) {
        Intent intent = new Intent();
        intent.setClass(context, LotteryBetDetailActivity.class);
        intent.putExtra("bet", betRecord);
        context.startActivity(intent);
    }

}
